#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
using namespace std;
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Error.h"

//g++ -std=gnu++11 -Wall -g Test_XRPCCore.cpp -o Test_XRPCCore -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_HelpComponents -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp -lNetEngine_BaseLib -lNetEngine_Algorithm -lNetEngine_Core -lNetEngine_ManagePool -lNetEngine_OPenSsl -lNetClient_Socket -lHelpComponents_Packet -lRfcComponents_HttpServer -lNetHelp_XRpc -lNetHelp_APIHelp -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_HelpComponents:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp,--disable-new-dtags

void __stdcall XRPCCore_Callback(LPCTSTR lpszClientAddr, BOOL bStream, LPCTSTR lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE nRtnType, list<NETHELP_XRPCPARAMETE>* pStl_ListParamete, LPVOID lParam)
{
    printf("Client:%s FuncName:%s\n", lpszClientAddr, lpszFuncName);

    int nMsgLen = 1024;
    TCHAR tszMsgBuffer[1024];
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

    if (0 == _tcsnicmp(lpszFuncName, _T("XEngine_AddMethod"), _tcslen(lpszFuncName)))
    {
        int nRet = 0;
        list<NETHELP_XRPCPARAMETE>::const_iterator stl_ListIterator = pStl_ListParamete->begin();
        for (; stl_ListIterator != pStl_ListParamete->end(); stl_ListIterator++)
        {
            int nValue = 0;
            memcpy(&nValue, stl_ListIterator->lParameteValue, stl_ListIterator->nParameteLen);

            nRet += nValue;
        }
        XRPCProtocol_Core_RepFuncCall(lpszFuncName, (LPCTSTR)&nRet, sizeof(int), nRtnType, tszMsgBuffer, &nMsgLen, bStream);
        NetHelp_XRPCCore_SendMsg(lpszClientAddr, tszMsgBuffer, nMsgLen, bStream);
    }
    if (0 == _tcsnicmp(lpszFuncName, _T("XEngine_Print"), _tcslen(lpszFuncName)))
    {
        list<NETHELP_XRPCPARAMETE>::const_iterator stl_ListIterator = pStl_ListParamete->begin();
        for (; stl_ListIterator != pStl_ListParamete->end(); stl_ListIterator++)
        {
            printf("XEngine_Print:%s\n",(LPCTSTR)stl_ListIterator->lParameteValue);
        }
        XRPCProtocol_Core_RepFuncCall(lpszFuncName, NULL, 0, nRtnType, tszMsgBuffer, &nMsgLen, bStream);
        NetHelp_XRPCCore_SendMsg(lpszClientAddr, tszMsgBuffer, nMsgLen, bStream);
    }
}
int main()
{
    LPCTSTR lpszCodeFile = _T("./HttpCode.types");
    LPCTSTR lpszMiniFile = _T("./HttpMime.types");

    if (!NetHelp_XRPCCore_Init(50001, 50002, lpszCodeFile, lpszMiniFile))
    {
        printf("NetHelp_XRPCCore_Init:%lX", XRpc_GetLastError());
    }
    NetHelp_XRPCCore_SetCallback(XRPCCore_Callback);

    printf("ok\n");

    while (1)
    {
        Sleep(5000);
    }
    NetHelp_XRPCCore_Destory();

    return 0;
}
